import http from '../common/request'
import { TERMINAL_TYPE } from '../common/enums'
import { Module } from 'vuex'
import { IFriend, friendApi } from '../api/friend'

export interface IFriendState {
  friends: IFriend[]
  timer?: NodeJS.Timeout
}

const friendStore: Module<IFriendState, any> = {
  state: {
    friends: [],
    timer: undefined
  },
  mutations: {
    setFriends(state, friends: IFriend[]) {
      state.friends = friends
    },

    updateFriend(state, friend: IFriend) {
      state.friends.forEach((f, index) => {
        if (f.id == friend.id) {
          // 拷贝属性
          const online = state.friends[index].online
          Object.assign(state.friends[index], friend)
          state.friends[index].online = online
        }
      })
    },

    removeFriend(state, id: number) {
      state.friends.forEach((f, idx) => {
        if (f.id == id) {
          state.friends.splice(idx, 1)
        }
      })
    },

    addFriend(state, friend: IFriend) {
      state.friends.push(friend)
    },

    setOnlineStatus(state, onlineTerminals: { userId: number; terminals: TERMINAL_TYPE[] }[]) {
      state.friends.forEach((f) => {
        const userTerminal = onlineTerminals.find((o) => f.id == o.userId)
        if (userTerminal) {
          f.online = true
          f.onlineTerminals = userTerminal.terminals
          f.onlineWeb = userTerminal.terminals.indexOf(TERMINAL_TYPE.WEB) >= 0
          f.onlineApp = userTerminal.terminals.indexOf(TERMINAL_TYPE.APP) >= 0
        } else {
          f.online = false
          f.onlineTerminals = []
          f.onlineWeb = false
          f.onlineApp = false
        }
      })
      state.friends.sort((f1, f2) => {
        if (f1.online && !f2.online) return -1
        if (f2.online && !f1.online) return 1
        return 0
      })
    },
    // 刷新状态
    refreshOnlineStatus(state) {
      if (state.friends.length > 0) {
        const userIds: number[] = []
        state.friends.forEach((f) => {
          userIds.push(f.id)
        })
        http({
          url: '/user/terminal/online?userIds=' + userIds.join(','),
          method: 'GET'
        }).then((onlineTerminals) => {
          this.commit('setOnlineStatus', onlineTerminals)
        })
      }
      // 30s后重新拉取
      clearTimeout(state.timer)
      state.timer = setTimeout(() => {
        this.commit('refreshOnlineStatus')
      }, 30000)
    },
    // 清除
    clear(state) {
      clearTimeout(state.timer)
      state.friends = []
      state.timer = undefined
    }
  },
  actions: {
    loadFriend(context) {
      return new Promise((resolve, reject) => {
        friendApi.getFriendList().then((friends) => {
          context.commit('setFriends', friends)
          context.commit('refreshOnlineStatus')
          resolve(friends)
        }).catch((error) => {
          reject(error)
        })
      })
    }
  }
}

export default friendStore
